﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class NguoiDungChiTieuDAO : SqlDAO
    {
        public NguoiDungChiTieuDAO(string connectionString, System.Type type = null) :
            base(connectionString, type) { ;}

        public DataPage<NguoiDungChiTieu> GetListPaging(NguoiDungChiTieu model, int pageIndex, int pageSize)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spNguoiDungChiTieu_GetListPaging");
                cmd.Parameters.Add(CreateParameter("@NAM_KE_HOACH_ID", model.NAM_KE_HOACH_ID));
                cmd.Parameters.Add(CreateParameter("@NGUOI_DUNG_ID", model.NGUOI_DUNG_ID));
                cmd.Parameters.Add(CreateParameter("@DON_VI_ID", model.DON_VI_ID));
                cmd.Parameters.Add(CreateParameter("@PAGE_INDEX", pageIndex));
                cmd.Parameters.Add(CreateParameter("@PAGE_SIZE", pageSize));

                return GetDataPage<NguoiDungChiTieu>(cmd, pageIndex, pageSize);
            }
            catch
            {
                throw;
            }
        }

        public List<NguoiDungChiTieu> GetListChiTieu(NguoiDungChiTieu model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spNguoiDungChiTieu_DanhSachChiIieu");
                cmd.Parameters.Add(CreateParameter("@NAM_KE_HOACH_ID", model.NAM_KE_HOACH_ID));
                cmd.Parameters.Add(CreateParameter("@NGUOI_DUNG_ID", model.NGUOI_DUNG_ID));
                cmd.Parameters.Add(CreateParameter("@DON_VI_ID", model.DON_VI_ID));

                return GetList<NguoiDungChiTieu>(cmd);
            }
            catch
            {

                throw;
            }
        }

        public bool Save(List<NguoiDungChiTieu> listData)
        {
            try
            {
                DataTable data = GetDataTable(listData);
                SqlCommand cmd = this.GetSprocCommand("spNguoiDungChiTieu_Insert");
                cmd.Parameters.Add(new SqlParameter("@Table", data));

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool Delete(int[] id)
        {
            try
            {
                DataTable listId = this.GetTableId<int>(id);
                SqlCommand cmd = this.GetSprocCommand("spNguoiDungChiTieu_Delete");
                cmd.Parameters.Add(new SqlParameter("@TableId", listId));

                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {

                throw;
            }
        }

        private DataTable GetDataTable(List<NguoiDungChiTieu> listData)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID");
            table.Columns.Add("NGUOI_DUNG_ID");
            table.Columns.Add("CHI_TIEU_ID");
            table.Columns.Add("MA_CAP1");
            table.Columns.Add("DON_VI_ID");
            table.Columns.Add("NAM_KE_HOACH_ID");
            NguoiDungChiTieu item = null;
            for (int i = 0; i < listData.Count; i++)
            {
                item = listData[i];
                table.Rows.Add(item.ID,
                               item.NGUOI_DUNG_ID,
                               item.CHI_TIEU_ID,
                               item.MA_CAP1,
                               item.DON_VI_ID,
                               item.NAM_KE_HOACH_ID);
            }

            return table;
        }
    }
}
